package com.jcloudsoft.springbootshardingsphere.ShardingAlgorithm;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.HashSet;

/**
 * @author wangfei
 * @date 2022-02-15 17:46
 */
public class MyStandardShardingAlgorithm implements StandardShardingAlgorithm {
    @Override
    public String doSharding(Collection collection, PreciseShardingValue preciseShardingValue) {

        //这里写具体的分片方法 shardingjdbcValue就是传过来的分片值，经过自己处理后返回相应的字符串即为所选片名
        System.out.println("preciseShardingValue.getColumnName() ----->"+preciseShardingValue.getColumnName());
        System.out.println("preciseShardingValue.getLogicTableName() ----->"+preciseShardingValue.getLogicTableName());
        System.out.println("preciseShardingValue.getValue() ----->"+preciseShardingValue.getValue());
        System.out.println("return ds0");
        Long userId = (Long)preciseShardingValue.getValue();
        return "ds" + userId%2;

    }

    @Override
    public Collection<String> doSharding(Collection collection, RangeShardingValue rangeShardingValue) {
        System.out.println("collection："+collection);
        System.out.println("rangeShardingValue："+rangeShardingValue);

        Collection<String> routTables = new HashSet<String>();
        routTables.add("ds0");
        return routTables;

    }

    @Override
    public void init() {

    }

    @Override
    public String getType() {
        return "CLASS_BASED";
    }
}
